
package com.lee.utils; 
 
import com.google.common.collect.Lists; 
import com.lee.source.HbaseSource; 
 
import java.io.IOException; 
import java.util.List; 
 
public class DCountUtils { 
    private static String tableName = "marketing:dcount";
    private static String family = "info";
    private static String qualifier = "info";
    private static List<String> indexKey = Lists.newArrayList("$index$");
    public static void initMap() throws IOException {
        HbaseSource.truncate(tableName);
        setValueByKey(indexKey,0l);
    }
    public static boolean judgeKeyInMap(List<String> key) throws IOException {
        key.sort(String::compareTo);
        String result = HbaseSource.get(key.toString(),tableName,family,qualifier);
        if(! "".equals(result)){
            return true;
        }else {
            return false;
        }
 
    }
    public static void setValueByKey(List<String> key,long value) throws IOException {
        key.sort(String::compareTo);
        String dataStr = String.valueOf(value);
        HbaseSource.put(key.toString(),tableName,family,qualifier,dataStr);
    }
 
    public static long getValueByKey(List<String> key) throws IOException {
        key.sort(String::compareTo);
        if(judgeKeyInMap(key)){
            String data = HbaseSource.get(key.toString(),tableName,family,qualifier);
            long dataD = Long.parseLong(data);
            return dataD;
        } else {
            return 0;
        }
 
    }
 
 
 
    public static long getIndex() throws IOException {
        if(judgeKeyInMap(indexKey)){
            long data = getValueByKey(indexKey);
            return data;
        } else {
            initMap();
            return 0;
        }
 
    }
 
    public static void addIndex() throws IOException {
        long index = getIndex();
        index += 1;
        setValueByKey(indexKey,index);
    }
 
} 
